package com.study.talk.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.study.talk.R;
import com.study.talk.entity.ChatCache;
import com.study.talk.net.OkHttp;

import java.util.List;
public class ChatCacheListViewAdapter extends BaseAdapter {

        List<ChatCache> data;
        private ShapeableImageView userListItemHeadImg;
        private TextView userListItemHeadUsername;
        private TextView userListItemHeadIntroduce;

        public ChatCacheListViewAdapter(List<ChatCache> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            @SuppressLint({"ViewHolder", "InflateParams"}) View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_list_item, null);
            initView(v);
            ChatCache cache = data.get(i);
            Glide.with(viewGroup.getContext()).load(OkHttp.URL+cache.getAvatar()).into(userListItemHeadImg);
            userListItemHeadUsername.setText(cache.getName());
            userListItemHeadIntroduce.setText(cache.getLastContent());
            return v;
        }

        private void initView(View view) {
            userListItemHeadImg = (ShapeableImageView) view.findViewById(R.id.user_list_item_head_img);
            userListItemHeadUsername = (TextView) view.findViewById(R.id.user_list_item_head_username);
            userListItemHeadIntroduce = (TextView) view.findViewById(R.id.user_list_item_head_introduce);
        }
}
